package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.auth.commons.DatabaseServiceStatus;
import com.redoor.app.stake.auth.commons.StakeApplicationStatus;
import com.redoor.app.stake.auth.model.BaseUser;
import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.dao.BaseChargerMapper;
import com.redoor.app.stake.basicdata.dao.BaseParkingSpaceMapper;
import com.redoor.app.stake.basicdata.model.BaseCharger;
import com.redoor.app.stake.basicdata.model.BaseParkingArea;
import com.redoor.app.stake.basicdata.model.BaseParkingSpace;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseParkingSpaceCriteria;
import com.redoor.app.stake.basicdata.model.serviceModel.BaseParkingSpaceServiceBean;
import com.redoor.app.stake.basicdata.service.BaseParkingAreaService;
import com.redoor.app.stake.basicdata.service.BaseParkingSpaceService;
import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoornetwork.framework.core.exception.ServiceException;
import com.redoornetwork.framework.core.response.CoreStatus;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * Created by wangjunlei on 2018-02-12 10:08:10.
 */
@Service
public class BaseParkingSpaceServiceImpl extends AbstractService<BaseParkingSpace, String> implements BaseParkingSpaceService {

    @Autowired
    private BaseParkingSpaceMapper baseParkingSpaceMapper;
    @Autowired
    private BaseUserService baseUserService;
    @Autowired
    private BaseParkingAreaService baseParkingAreaService;
    @Autowired
    private BaseChargerMapper baseChargerMapper;
    @Override
    public List<BaseParkingSpaceServiceBean> qryParkingSpaceByParkingAreaUuidOrderByBindCharger(String parkingAreaUuid) {
        if(StringUtils.isBlank(parkingAreaUuid)){
            throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
        }
        // 获取桩所属停车区所有的停车位
        List<BaseParkingSpaceServiceBean> allParkingSpace = this.baseParkingSpaceMapper.qryParkingSpaceByParkingAreaUuid(parkingAreaUuid);
        //获取桩所属停车区所有已经绑定枪的停车位
        List<BaseParkingSpaceServiceBean> bindedChargerParkingSpace = this.baseParkingSpaceMapper.qryParkingSpaceBindedChargerByParkingAreaUuid(parkingAreaUuid);
        // 所有停车位中移除已经绑定枪的停车位
        allParkingSpace.removeAll(bindedChargerParkingSpace);

        // 写入未绑定枪的标记
        for (BaseParkingSpaceServiceBean bean : allParkingSpace){
            bean.setBindedCharger(0);
        }
        // 写入已经绑定枪的标记
        for (BaseParkingSpaceServiceBean bean : bindedChargerParkingSpace){
            bean.setBindedCharger(1);
        }
        // 写入标记之后合并
        allParkingSpace.addAll(bindedChargerParkingSpace);
        return allParkingSpace;
    }

    @Override
    public List<BaseParkingSpaceServiceBean> qryParkingSpaceList(BaseParkingSpaceCriteria criteria) {
        return this.baseParkingSpaceMapper.qryParkingSpaceList(criteria);
    }

    @Override
    public int qryParkingSpaceCnt(BaseParkingSpaceCriteria criteria) {
        return this.baseParkingSpaceMapper.qryParkingSpaceCnt(criteria) ;
    }

    @Override
    public BaseParkingSpace savePrakSpace(BaseParkingSpace baseParkingSpace) {
        //  传参为空,抛出异常
        if (null == baseParkingSpace) {
            throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
        }
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        /*uuid为空,新增一条停车位信息*/
        if(StringUtils.isBlank(baseParkingSpace.getUuid())){
            /*若停车位所属停车区uuid为空,则不能生成停车位编码*/
            if (StringUtils.isBlank(baseParkingSpace.getParkingareauuid())){
                throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
            }
            baseParkingSpace.setUuid(UUIDTool.getUUID());
            baseParkingSpace.setParkno(this.genParkSpaceCode(baseParkingSpace.getParkingareauuid()));
            baseParkingSpace.setCreatedat(new Date());
            baseParkingSpace.setCreateduuid(loginUser.getUuid());
            baseParkingSpace.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
            /*保存成功*/
            if(baseParkingSpaceMapper.insert(baseParkingSpace) > 0){
                return baseParkingSpace;
            }else {
                throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
            }

        /*uuid不为空,跟新该停车位信息*/
        }else {
            BaseParkingSpace oldParkspace = this.findById(baseParkingSpace.getUuid());
            if(oldParkspace == null){
                throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
            }else{
                baseParkingSpace.setUpdaterat(new Date());
                baseParkingSpace.setUpdateruuid(loginUser.getUuid());
                baseParkingSpace.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
                /*保存成功*/
                if(baseParkingSpaceMapper.updateByPrimaryKeySelective(baseParkingSpace) > 0){
                    return baseParkingSpace;
                }else {
                    throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
                }
            }
        }
    }

    @Override
    public int delParkSpace(String uuid) {
        BaseUser loginUser = baseUserService.getShiroUser(null);
        BaseParkingSpace baseParkingSpace = this.findById(uuid);
        if (null != baseParkingSpace){
            baseParkingSpace.setUpdaterat(new Date());
            baseParkingSpace.setUpdateruuid(loginUser.getUuid());
            baseParkingSpace.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_DEL_STATUS.getCode()));
            return baseParkingSpaceMapper.updateByPrimaryKeySelective(baseParkingSpace);
        }else {
            throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
        }
    }

    @Override
    public String genParkSpaceCode(String parkingAreaUuid) {
        /*通过停车区编码生成停车位编码*/
        class genParkingSpaceCodeByParkingAreaCode{
            public String genCode(String parkingAreaUuid,int parkingSpaceListSize){
                BaseParkingArea parkingArea = baseParkingAreaService.findById(parkingAreaUuid);
                if (null == parkingArea){
                    throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
                }
                /*停车位编码 = 停车区编码 + 0xx(该停车区下停车位数量加一,不够三位数用0在前面补全)*/
                return parkingArea.getCode() + new DecimalFormat("000").format(parkingSpaceListSize+1);
            }
        }

        if (StringUtils.isBlank(parkingAreaUuid)){
            throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
        }
        List<String> parkingSpaceCodesList = baseParkingSpaceMapper.qryParkingSpaceCodeByParkingAreaUuid(parkingAreaUuid);
        return new genParkingSpaceCodeByParkingAreaCode().genCode(parkingAreaUuid,parkingSpaceCodesList.size());

//  注释掉的原因: 本打算查停车位编码然后拿到最大值直接递增生成新的编码,但是考虑到拿到的最大值可能不是按照上面的规则生成的,所以注释掉了.

//        /*若停车区下没有停车位,根据停车区编码生成停车位编码*/
//        if(parkingSpaceCodesList.isEmpty()){
//            return new genParkingSpaceCodeByParkingAreaCode().genCode(parkingAreaUuid,parkingSpaceCodesList.size());
//
//        }else {
//            Collections.sort(parkingSpaceCodesList);
//            /*取到最大的停车位编码*/
//            String maxParkno = parkingSpaceCodesList.get(parkingSpaceCodesList.size()-1);
//            /*最大停车位编码能转成Integer,用最大编码加一生成新增停车位编码*/
//            if (NumberUtils.isCreatable(maxParkno)){
//                return (Integer.parseInt(maxParkno) + 1) + "";
//            /*最大停车位编码不能转成Integer,调用用停车区编码生成停车位编码*/
//            }else{
//                return new genParkingSpaceCodeByParkingAreaCode().genCode(parkingAreaUuid,parkingSpaceCodesList.size());
//            }
//        }

    }

    @Override
    public BaseParkingSpaceServiceBean qryParkingSpaceByUuid(String uuid) {
        return this.baseParkingSpaceMapper.qryParkingSpaceByUuid(uuid);
    }

    @Override
    public int qryParkingSpaceCountByName(String name) {
        BaseParkingSpace baseParkingSpace = new BaseParkingSpace();
        baseParkingSpace.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        baseParkingSpace.setName(name);
        return this.baseParkingSpaceMapper.selectCount(baseParkingSpace);
    }

    @Override
    public int qryParkingBingChanage(String uuid) {
        BaseParkingSpace baseParkingSpace = new BaseParkingSpace();
        baseParkingSpace.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        baseParkingSpace.setParkingareauuid(uuid);
        List<BaseParkingSpace> baseParkingSpaceList = this.baseParkingSpaceMapper.select(baseParkingSpace);
        //当停车区下有停车位的时候进行判断
        if(baseParkingSpaceList.size() >0) {
            //计数器
            int i = 0;
            for (BaseParkingSpace bean : baseParkingSpaceList) {
                BaseCharger baseCharger = new BaseCharger();
                baseCharger.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
                baseCharger.setSpaceuuid(baseCharger.getUuid());
                //查询停车位绑定的枪
                i += this.baseChargerMapper.selectCount(baseCharger);
            }
            //当停车位绑定有枪的时候返回整形状态0(未进行区的删除)
            if (i > 0) {
                return 2;
            }else {
                return 1;
            }
        }else{
            return 0;
        }
    }

}
